package FourArithmeticOperations;

import Try.Original;

import java.beans.Expression;
import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 * Created by dell on 2017/10/27.
 */
public class Count {
    BTNode<String> root;
    public Count(){
        root = null;
    }
    public BTNode<String> ExpressionT(String str){
        StringTokenizer tokenizer = new StringTokenizer(str);
        ArrayList<LinkedBinaryTree<String>> num = new ArrayList<>();
        ArrayList<String> fuhao = new ArrayList<>();
        String token;

        while (tokenizer.hasMoreTokens()){
            token = tokenizer.nextToken();

            if(as(token)){
                fuhao.add(token);
            }else if(md(token)){
                LinkedBinaryTree<String> left = num.remove(num.size()-1);
                String A = token;
                token = tokenizer.nextToken();
                LinkedBinaryTree<String> right = new LinkedBinaryTree<String>(token);
                num.add(new LinkedBinaryTree<String>(A,left,right));
            }else
                num.add(new LinkedBinaryTree(token));
        }
        while (fuhao.size()!=0){
            num.add(0,new LinkedBinaryTree<>(fuhao.remove(0),num.remove(0),num.remove(0)));
        }
        this.root = num.remove(0).root;
        return root;
    }

    private boolean as(String token){
        return (token.equals("+")||token.equals("-"));
    }

    private boolean md(String token){
        return (token.equals("*")||token.equals("/"));
    }
}
